package com.rentcars.controller;

import com.github.pagehelper.Page;
import com.rentcars.entity.Cars1;
import com.rentcars.entity.Result;
import com.rentcars.service.Cars1Service;
import com.rentcars.utils.DateUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 汽车管理
 * @author 浩
 */
@RestController
public class CarsController {
    @Getter
    @Setter
    @Autowired
    private Cars1Service cars1Service;

    @RequestMapping("/carGetPage")
    public Map queryRepair(int pageNum, int pageSize, Cars1 cars1) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("cars", cars1);
        Page page = cars1Service.qureyUsersByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/carsAdd")
    public Result add(Cars1 cars1) {
        String picid1 = cars1.getPicid();
        String picid = picid1.substring(12);
        cars1.setPicid(picid);
        cars1Service.insert(cars1);
        return new Result();
    }

    // 上传文件
    @ResponseBody
    @RequestMapping("/upload")
    public String fileUpload(@RequestParam("files") MultipartFile files) throws IOException {
        File projectPath = new File(ResourceUtils.getURL("classpath:").getPath());
        File pathFile = new File(projectPath.getAbsolutePath(), "public/img/");
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        File serverFile = new File(pathFile, files.getOriginalFilename());
        files.transferTo(serverFile);
        String imgPath = (files.getOriginalFilename());
        return imgPath;
    }

    @RequestMapping("/carsDel")
    public Result myEdit(String carid) {
        cars1Service.deleteByPrimaryKey(carid);
        return new Result();
    }

    @RequestMapping("/carsGet")
    public ModelAndView edit(String carid) throws Exception {
        Cars1 cars1 = cars1Service.selectByPrimaryKey(carid);
        Date buydate = DateUtils.getDate(cars1.getBuydate());
        Date insurancetime = DateUtils.getDate(cars1.getInsurancetime());
        Date yearcheck = DateUtils.getDate(cars1.getYearcheck());
        Date maintaintime = DateUtils.getDate(cars1.getMaintaintime());
        Map<String, Object> map = new HashMap<>();
        map.put("carid",carid);
        map.put("carcode", cars1.getCarcode());
        map.put("carcolor", cars1.getCarcolor());
        map.put("cartype", cars1.getCartype());
        map.put("carbrand", cars1.getCarbrand());
        map.put("carbrandtype", cars1.getCarbrandtype());
        map.put("picid", cars1.getPicid());
        map.put("seatnum", cars1.getSeatnum());
        map.put("totalmile", cars1.getTotalmile());
        map.put("hadillegal", cars1.getHadillegal());
        map.put("insurer", cars1.getInsurer());
        map.put("maintainmile", cars1.getMaintainmile());
        map.put("safelevel", cars1.getSafelevel());
        map.put("carprice", cars1.getCarprice());
        map.put("evaluater", cars1.getEvaluater());
        map.put("carstate", cars1.getCarstate());
        map.put("deptname", cars1.getDeptname());
        map.put("carremark", cars1.getCarremark());
        map.put("carcity", cars1.getCarcity());
        map.put("budate", buydate);
        map.put("insurancetime", insurancetime);
        map.put("yearcheck", yearcheck);
        map.put("maintaintime", maintaintime);
        return new ModelAndView("car-edit",map);
    }
    @RequestMapping("/carsEdit")
    public Result myEdit(Cars1 cars1){
        System.out.println(cars1.getPicid());
        if (cars1.getPicid()!=""){
            String picid1 = cars1.getPicid();
            String picid = picid1.substring(12);
            cars1.setPicid(picid);
        }
        cars1Service.updateByPrimaryKey(cars1);
        return new Result();
    }
    //主页面
    @RequestMapping("c4")
    public ModelAndView getCars3() {
        return new ModelAndView("car-list");
    }

    @RequestMapping("/c1")
    public ModelAndView getCar(){
        return new ModelAndView("car-addd");
    }

}
